package io.hansan.oli.controller;
import io.hansan.oli.entity.InboundOrder;
import io.hansan.oli.service.InboundOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.JAXBException;
/**
 * @Author ：何汉叁
 * @Date ：2025/5/22 16:44
 * @Description：TODO
 */

@RestController
@RequestMapping("/api/v1/inbound-orders")
public class InboundOrderController {
   @Autowired
   private InboundOrderService inboundOrderService;

   /**
    * 处理入库订单XML报文
    *
    * @param xmlContent XML报文内容
    * @return 处理后的入库订单信息
    */
   @PostMapping("/add")
   public ResponseEntity<?> processOrder(@RequestBody String xmlContent) {
       if (xmlContent == null) {
           return ResponseEntity.badRequest().body("XML内容不能为空");
       }

       try {
           InboundOrder order = inboundOrderService.processXmlOrder(xmlContent);
           return ResponseEntity.ok(order);
       } catch (JAXBException e) {
           return ResponseEntity.badRequest()
                   .body("处理XML报文失败: " + e.getMessage());
       }
   }

   /**
    * 根据订单ID查询入库订单
    *
    * @param orderId 订单ID
    * @return 入库订单信息，若未找到则返回404
    */
   @GetMapping("/{orderId}")
   public ResponseEntity<InboundOrder> getByOrderId(@PathVariable String orderId) {
       if (orderId == null) {
           return ResponseEntity.badRequest().build();
       }

       InboundOrder order = inboundOrderService.getByOrderId(orderId);
       return order != null ? ResponseEntity.ok(order) : ResponseEntity.notFound().build();
   }
}